package com.example.pharmacypick.controller;


import com.example.pharmacypick.dto.PatientDto;
import com.example.pharmacypick.pojo.LoginRequest;
import com.example.pharmacypick.pojo.Patient;
import com.example.pharmacypick.service.PatientService;
import com.example.pharmacypick.utils.RespBean;
import com.example.pharmacypick.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/patient")
public class PatientController {


    @Autowired
    PatientService patientService;

    /**
     * 分页查询病人信息
     * @param patient_name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public RespBean findAllPatients(@RequestParam String patient_name,@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        try {
            PatientDto patientDto=new PatientDto();
            patientDto.setPatientName(patient_name);
            patientDto.setPageNum(pageNum);
            patientDto.setPageSize(pageSize);
            PageInfo<Patient> allPatients = patientService.findAllPatients(patientDto);
            return RespBean.success("成功",allPatients);
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }

    @PostMapping
    public RespBean addPatient(@RequestBody PatientDto patientDto){
        try {
            patientService.addPatient(patientDto);
            return RespBean.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }


    @PutMapping
    public RespBean updatePatient(@RequestBody PatientDto patientDto){
        try {
            patientService.updatePatient(patientDto);
            return RespBean.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }

    @DeleteMapping("/{patientRecord}")
    public RespBean deletePatient(@PathVariable Long patientRecord){
        try {
            patientService.deletePatient(patientRecord);
            return RespBean.success("成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }


    @PostMapping("/login")
    public RespBean patientLogin(@RequestBody LoginRequest loginRequest){
        Patient patient=new Patient();
        patient.setPatientRecord(loginRequest.getPassword());
        patient.setPatientName(loginRequest.getUsername());

        Patient patientLogin = patientService.patientLogin(patient);

        if (patientLogin==null){
            return RespBean.error("error");
        }

        return RespBean.success("成功",patientLogin.getPatientRecord());
    }

    @GetMapping("/patientRecord")
    public RespBean findPatientByPatientRecord(@RequestParam int patientRecord){
        try {
            Patient patientByPatientRecord = patientService.findPatientByPatientRecord(patientRecord);
            return RespBean.success("成功",patientByPatientRecord);
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.error("error");
        }
    }

}
